<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Redis 集群脑裂问题深度解析</title>
    <link href="https://cdn.staticfile.org/font-awesome/6.4.0/css/all.min.css" rel="stylesheet">
    <link href="https://cdn.staticfile.org/tailwindcss/2.2.19/tailwind.min.css" rel="stylesheet">
    <link href="https://fonts.googleapis.com/css2?family=Noto+Serif+SC:wght@400;500;600;700&family=Noto+Sans+SC:wght@300;400;500;700&display=swap" rel="stylesheet">
    <script src="https://cdn.jsdelivr.net/npm/mermaid@latest/dist/mermaid.min.js"></script>
    <style>
        body {
            font-family: 'Noto Sans SC', Tahoma, Arial, Roboto, "Droid Sans", "Helvetica Neue", "Droid Sans Fallback", "Heiti SC", "Hiragino Sans GB", Simsun, sans-serif;
            background: linear-gradient(135deg, #f5f7fa 0%, #c3cfe2 100%);
            min-height: 100vh;
        }
        
        .hero-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
        }
        
        .card-hover {
            transition: all 0.3s ease;
            border: 1px solid transparent;
        }
        
        .card-hover:hover {
            transform: translateY(-5px);
            box-shadow: 0 20px 40px rgba(0,0,0,0.1);
            border-color: #667eea;
        }
        
        .text-gradient {
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            -webkit-background-clip: text;
            -webkit-text-fill-color: transparent;
            background-clip: text;
        }
        
        .section-title {
            position: relative;
            padding-left: 20px;
        }
        
        .section-title::before {
            content: '';
            position: absolute;
            left: 0;
            top: 50%;
            transform: translateY(-50%);
            width: 4px;
            height: 24px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            border-radius: 2px;
        }
        
        .first-letter {
            float: left;
            font-size: 4rem;
            line-height: 3rem;
            padding-right: 8px;
            margin-top: 3px;
            font-weight: 700;
            color: #667eea;
        }
        
        .icon-box {
            width: 60px;
            height: 60px;
            display: flex;
            align-items: center;
            justify-content: center;
            border-radius: 12px;
            background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
            color: white;
            font-size: 24px;
        }
        
        .mermaid {
            display: flex;
            justify-content: center;
            margin: 2rem 0;
        }
        
        code {
            background: #f3f4f6;
            padding: 2px 6px;
            border-radius: 4px;
            font-family: 'Consolas', 'Monaco', monospace;
            color: #e11d48;
        }
    </style>
</head>
<body>
    <!-- Hero Section -->
    <div class="hero-gradient text-white py-20">
        <div class="container mx-auto px-6">
            <div class="max-w-4xl mx-auto text-center">
                <h1 class="text-5xl font-bold mb-6">Redis 集群脑裂问题</h1>
                <p class="text-xl opacity-90 mb-8">深入理解分布式系统中的数据一致性挑战</p>
                <div class="flex justify-center space-x-4">
                    <div class="flex items-center">
                        <i class="fas fa-brain mr-2"></i>
                        <span>脑裂现象</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-network-wired mr-2"></i>
                        <span>网络分区</span>
                    </div>
                    <div class="flex items-center">
                        <i class="fas fa-shield-alt mr-2"></i>
                        <span>解决方案</span>
                    </div>
                </div>
            </div>
        </div>
    </div>

    <!-- Main Content -->
    <div class="container mx-auto px-6 py-12">
        <div class="max-w-5xl mx-auto">
            
            <!-- Introduction Card -->
            <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
                <p class="text-lg leading-relaxed text-gray-700">
                    <span class="first-letter">R</span>edis 集群中的"脑裂"问题（split-brain）是指在集群中出现多个主节点（master）之间的分裂状态，导致集群中的不同主节点互相独立地认为自己是唯一的主节点。这种情况通常发生在网络分区或故障时，可能会导致数据不一致和系统的不稳定。
                </p>
            </div>

            <!-- Definition Section -->
            <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
                <h2 class="text-3xl font-bold mb-6 section-title">一、脑裂问题的定义</h2>
                <p class="text-gray-700 mb-6">在 Redis 集群中，脑裂现象通常发生在以下情况下：</p>
                
                <div class="grid md:grid-cols-2 gap-6">
                    <div class="flex items-start space-x-4">
                        <div class="icon-box">
                            <i class="fas fa-network-wired"></i>
                        </div>
                        <div>
                            <h3 class="text-xl font-semibold mb-2">网络分区</h3>
                            <p class="text-gray-600">集群中的节点因网络问题被分隔成多个子集，每个子集中的节点无法与其他子集中的节点通信。</p>
                        </div>
                    </div>
                    
                    <div class="flex items-start space-x-4">
                        <div class="icon-box">
                            <i class="fas fa-server"></i>
                        </div>
                        <div>
                            <h3 class="text-xl font-semibold mb-2">节点故障</h3>
                            <p class="text-gray-600">主节点发生故障或无法与集群中的其他节点通信，但故障主节点的从节点（slave）仍在继续处理请求。</p>
                        </div>
                    </div>
                </div>
                
                <div class="mt-6 p-4 bg-purple-50 rounded-lg">
                    <p class="text-gray-700">
                        <i class="fas fa-exclamation-triangle text-purple-600 mr-2"></i>
                        在脑裂的情况下，两个或多个主节点可能会同时接受写操作，导致数据的不一致。
                    </p>
                </div>
            </div>

            <!-- Visualization -->
            <div class="bg-white rounded-2xl shadow-xl p-8 mb-8">
                <h3 class="text-2xl font-bold mb-6 text-center text-gradient">脑裂现象可视化</h3>
                <div class="mermaid">
                    graph TB
                        subgraph "正常状态"
                            M1[主节点 Master]
                            S1[从节点 Slave1]
                            S2[从节点 Slave2]
                            C1[客户端]
                            M1 --> S1
                            M1 --> S2
                            C1 --> M1
                        end
                        
                        subgraph "脑裂状态"
                            M2[原主节点]
                            M3[新主节点]
                            S3[从节点]
                            C2[客户端A]
                            C3[客户端B]
                            M2 -.网络分区.-> M3
                            C2 --> M2
                            C3 --> M3
                            M3 --> S3
                        end
                        
                        style M1 fill:#667eea,stroke:#fff,color:#fff
                        style M2 fill:#ef4444,stroke:#fff,color:#fff
                        style M3 fill:#ef4444,stroke:#fff,color:#fff
                </div>
            </div>

            <!-- Impact Section -->
            <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
                <h2 class="text-3xl font-bold mb-6 section-title">二、脑裂问题的影响</h2>
                
                <div class="space-y-6">
                    <div class="flex items-start space-x-4">
                        <div class="flex-shrink-0 w-10 h-10 bg-red-100 rounded-full flex items-center justify-center">
                            <span class="text-red-600 font-bold">1</span>
                        </div>
                        <div>
                            <h3 class="text-xl font-semibold mb-2">数据不一致</h3>
                            <p class="text-gray-600">由于不同的主节点可能同时接受写操作，导致同一数据的不同副本之间产生不一致。</p>
                        </div>
                    </div>
                    
                    <div class="flex items-start space-x-4">
                        <div class="flex-shrink-0 w-10 h-10 bg-red-100 rounded-full flex items-center justify-center">
                            <span class="text-red-600 font-bold">2</span>
                        </div>
                        <div>
                            <h3 class="text-xl font-semibold mb-2">系统不稳定</h3>
                            <p class="text-gray-600">系统中的多个主节点可能会引发冲突和混乱，使得集群状态变得不稳定。</p>
                        </div>
                    </div>
                    
                    <div class="flex items-start space-x-4">
                        <div class="flex-shrink-0 w-10 h-10 bg-red-100 rounded-full flex items-center justify-center">
                            <span class="text-red-600 font-bold">3</span>
                        </div>
                        <div>
                            <h3 class="text-xl font-semibold mb-2">难以恢复</h3>
                            <p class="text-gray-600">脑裂后的恢复过程可能需要人工干预，恢复操作复杂且风险较高。</p>
                        </div>
                    </div>
                </div>
            </div>

            <!-- Solutions Section -->
            <div class="bg-white rounded-2xl shadow-xl p-8 mb-8 card-hover">
                <h2 class="text-3xl font-bold mb-8 section-title">三、如何解决脑裂问题</h2>
                
                <div class="space-y-8">
                    <!-- Solution 1 -->
                    <div class="border-l-4 border-purple-500 pl-6">
                        <h3 class="text-xl font-semibold mb-3">
                            <i class="fas fa-cog text-purple-500 mr-2"></i>
                            使用 <code>cluster-require-full-coverage</code> 参数
                        </h3>
                        <p class="text-gray-700 leading-relaxed">
                            Redis 集群的 <code>cluster-require-full-coverage</code> 配置参数决定了集群在发生脑裂时是否要求所有槽（slots）都有主节点。如果该参数设置为 <code>yes</code>，当集群中有任意槽没有主节点时，集群会认为自己不可用，从而避免出现脑裂现象。这可以确保只有在集群中的所有主节点都可用时，系统才会继续提供服务。
                        </p>
                    </div>

                    <!-- Solution 2 -->
                    <div class="border-l-4 border-purple-500 pl-6">
                        <h3 class="text-xl font-semibold mb-3">
                            <i class="fas fa-sync-alt text-purple-500 mr-2"></i>
                            配置 <code>failover</code> 策略
                        </h3>
                        <div class="space-y-4 ml-4">
                            <div>
                                <h4 class="font-semibold text-gray-800">自动故障转移</h4>
                                <p class="text-gray-600">在 Redis 集群中，当一个主节点发生故障时，从节点会自动提升为新的主节点。这种机制可以减少因主节点故障引发的脑裂问题。确保故障转移（failover）机制正确配置并有效运行。</p>
                            </div>
                            <div>
                                <h4 class="font-semibold text-gray-800">手动干预</h4>
                                <p class="text-gray-600">如果自动故障转移无法处理脑裂问题，需要人工干预来修复集群的状态，手动重新配置主节点和从节点。</p>
                            </div>
                        </div>
                    </div>

                    <!-- Solution 3 -->
                    <div class="border-l-4 border-purple-500 pl-6">
                        <h3 class="text-xl font-semibold mb-3">
                            <i class="fas fa-network-wired text-purple-500 mr-2"></i>
                            增强网络稳定性
                        </h3>
                        <div class="space-y-4 ml-4">
                            <div>
                                <h4 class="font-semibold text-gray-800">网络隔离</h4>
                                <p class="text-gray-600">将 Redis 集群的节点放置在网络隔离良好的环境中，减少网络分区发生的概率。</p>
                            </div>